<template>
    <div class="app-container calendar-list-container">

        <!-- 查询和其他操作 -->
        <div class="filter-container">
          <el-input clearable class="filter-item" style="width: 200px;" placeholder="菜品编号" v-model="listQuery.dishesId"></el-input>
          <el-input clearable class="filter-item" style="width: 200px;" placeholder="菜品名称" v-model="listQuery.dishesName"></el-input>
          <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
          <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-edit">添加</el-button>
        </div>

        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
        
          <el-table-column align="center" min-width="120px" label="平台编号" prop="platformId">
          </el-table-column>
          <el-table-column align="center" min-width="120px" label="商户账号" prop="merId">
          </el-table-column>
          <el-table-column align="center" min-width="80px" label="菜品编号" prop="dishesId">
          </el-table-column>       
          <el-table-column align="center" min-width="80px" label="菜品类目编号" prop="categoryId">
          </el-table-column>      
          <el-table-column align="center" min-width="120px" label="菜品名称" prop="dishesName">
          </el-table-column>    
          <el-table-column align="center" min-width="200px" label="说明" prop="instructions">
          </el-table-column>    
          <el-table-column align="center" min-width="200px" label="图片地址" prop="imageUrl">
          </el-table-column>    
          <el-table-column align="center" min-width="80px" label="预定数量" prop="orderNum">
          </el-table-column>    
          <el-table-column align="center" min-width="80px" label="价格" prop="price">
          </el-table-column>    
          <el-table-column align="center" min-width="80px" label="规格开关" prop="isSpecification">
          </el-table-column>    
          <el-table-column align="center" min-width="80px" label="业务类型" prop="type">
            <template slot-scope="scope">
              <span v-if="scope.row.type == '1'">点餐</span>
              <span v-if="scope.row.type == '2'">外卖</span>
              <span v-if="scope.row.type == '3'">公共</span>
            </template>
          </el-table-column>     
          <el-table-column align="center" label="操作" width="230" >
              <template slot-scope="scope">
                <el-button type="danger" size="mini"  @click="handleDelete(scope.row)">删除</el-button>
                <el-button type="danger" size="mini"  @click="handleUpdate(scope.row)">修改</el-button>
              </template>
          </el-table-column> 

        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
            :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        </div>

        <!-- 修改对话框 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="addDialogFormVisible">
          <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="120px" style='width: 400px; margin-left:50px;'>
            <el-form-item label="平台编号" prop="platformId">
              <el-input v-model="dataForm.platformId" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="商户账号" prop="merId">
              <el-input v-model="dataForm.merId" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="菜品类目编号" prop="categoryId"> 
              <el-select v-model="dataForm.categoryId" placeholder="请选择" size="small"  clearable="">
                <el-option v-for="(c,index) in categorys" :label="c.name" :value="c.categoryId" :key="index"></el-option>
              </el-select> 
            </el-form-item>         
            <el-form-item label="菜品名称" prop="dishesName"> 
              <el-input v-model="dataForm.dishesName"></el-input>
            </el-form-item>     
            <el-form-item label="说明" prop="instructions"> 
              <el-input v-model="dataForm.instructions"></el-input>
            </el-form-item>     
            <el-form-item label="图片" prop="imageUrl"> 
              <div  @click="upCover">
                <img v-if="dataForm.imageUrl" :src="dataForm.imageUrl">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </div>
              <input type="file" name="file" id="upload_cover" @change="uploadCover" accept="image/png,image/gif" style="display: none">
            </el-form-item>        
            <el-form-item label="价格" prop="price"> 
              <el-input v-model="dataForm.price"></el-input>
            </el-form-item>        
            <el-form-item label="规格开关" prop="isSpecification"> 
              <el-input v-model="dataForm.isSpecification"></el-input>
            </el-form-item>     
            <el-form-item label="业务类型" prop="type"> 
              <el-select v-model="dataForm.type" placeholder="请选择" clearable="">
                <el-option label="点餐" value="1"></el-option>
                <el-option label="外卖" value="2"></el-option>
                <el-option label="公共" value="3"></el-option>
              </el-select>
            </el-form-item>           
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogFormVisible = false">取消</el-button>
            <el-button v-if="dialogStatus == 'create'" type="primary" @click="createData('dataForm')">确定</el-button>
            <el-button v-if="dialogStatus == 'update'" type="primary" @click="updateData('dataForm')">确定</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import waves from '@/directive/waves' // 水波纹指令
import { listDishesInfo, deleteDishesInfo, updateDishesInfo, addDishesInfo } from '@/api/cms/restaurant/dishes'
import { listDishesCategoryInfo } from '@/api/cms/restaurant/dishesCategory'
import { mapGetters } from 'vuex'
import { uploadImg } from '@/api/cms/society/index'

export default {
  name: 'auction',
  directives: {
    waves
  },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        dishesId: '',
        dishesName: ''
      },
      list: [],
      total: 0,
      categorys: [],
      listLoading: false,
      addDialogFormVisible: false,
      downloadLoading: false,
      dataForm: {
        platformId: '',
        merId: '',
        categoryId: '',
        dishesName: '',
        instructions: '',
        imageUrl: '',
        orderNum: '0',
        price: '',
        isSpecification: '',
        type: ''
      },
      rules: {
        categoryId: [
          {
            required: true,
            message: '请选择类目',
            trigger: 'blur'
          }
        ],
        dishesName: [
          {
            required: true,
            message: '请输入菜品名称',
            trigger: 'blur'
          }
        ],
        // imageUrl: [
        //   {
        //     required: true,
        //     message: '请上传菜品图片',
        //     trigger: 'blur'
        //   }
        // ],
        price: [
          {
            required: true,
            message: '请输入价格',
            trigger: 'blur'
          },
          {
            pattern: /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
            message: '输入格式错误，请重新输入!'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择类型',
            trigger: 'blur'
          }
        ],
        orderNum: [
          {
            pattern: /^[0-9]*$/,
            message: '输入格式错误，请重新输入!'
          }
        ]
      },
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogStatus: ''
    }
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'platformId'
    ])
  },
  created() {
    this.getList()
    this.getCategorys()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    getList() {
      this.listLoading = true
      listDishesInfo(this.listQuery).then(response => {
        console.log(response)
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    getCategorys() {
      var str = {}
      str.platformId = this.platformId
      listDishesCategoryInfo(str).then(response => {
        console.log(response)
        this.categorys = response.body.rows
      }).catch(() => {
        this.categorys = []
      })
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dataForm.platformId = this.platformId
      this.addDialogFormVisible = true
    },
    createData(formName) {
      const set = this.$refs
      set[formName].validate(valid => {
        if (valid) {
          this.$confirm('此操作将添加菜品, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.addDialogFormVisible = false
            this.listLoading = true
            addDishesInfo(this.dataForm).then((response) => {
              if (response.success) {
                this.$notify({
                  title: '成功',
                  message: '创建成功',
                  type: 'success',
                  duration: 5000
                })
                this.getList()
              }
              this.listLoading = false
            })
          })
        } else {
          return false
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将删除选中项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteDishesInfo(row)
            .then(response => {
              console.log(response)
              if (response.success) {
                this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              }
            })
        })
    },
    handleUpdate(row) {
      this.dialogStatus = 'update'
      this.dataForm = row
      this.addDialogFormVisible = true
    },
    updateData() {
      this.dataForm.categoryId = this.dataForm.categoryId.toString()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDishesInfo(this.dataForm).then(response => {
            this.getList()
            this.addDialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    upCover: function() {
      document.getElementById('upload_cover').click()
    },
    uploadCover(event) {
      if (event.target.files.length > 0) {
        this.file = event.target.files[0] // 提交的图片
        console.log(this.file)
        const param = new FormData() // 创建form对象
        param.append('file', this.file)
        uploadImg(param).then(response => {
          if (response.success) {
            this.dataForm.imageUrl = response.data.filePath
            alert('ok')
          }
        })
      }
    },
    resetForm() {
      this.dataForm = {
        platformId: '',
        merId: '',
        categoryId: '',
        dishesName: '',
        instructions: '',
        imageUrl: '',
        orderNum: '0',
        price: '',
        isSpecification: '',
        type: ''
      }
    }
  }
}
</script>

<style>

</style>


